package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.OrdersRefundBO;
import com.junzhiit.nengji.dao.OrdersRefundDAO;
import com.junzhiit.nengji.model.OrdersRefund;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrdersRefundStatus;

@Repository
public class OrdersRefundDAOImpl extends AbstractBaseDAO<OrdersRefund> implements OrdersRefundDAO {

	@Override
	public List<OrdersRefundBO> findAll(OrdersRefundBO ordersRefundBO, int pageNumber, int maxPageRow) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select r.*,o.order_number,g.name 'goods_name',");
		sql.append("u.nick_name,u.mobile_phone,");
		sql.append("s.name 'shop_name',s.mobile_phone 'shop_phone',");
		sql.append("g.price,og.sku_content_name,og.number,o.coupons_money,o.postage,o.pay_money,");
		sql.append("og.is_refund_goods ");
		sql.append(" from orders_refund r");
		sql.append(" inner join orders_goods og on og.id = r.orders_goods_id");
		sql.append(" inner join orders o on o.id = og.order_id");
		sql.append(" inner join goods g on g.id = og.goods_id");
		sql.append(" left join user u on u.id = r.user_id");
		sql.append(" left join shop s on s.id = r.shop_id");
		sql.append(" where r.is_delete = ?");
		params.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(ordersRefundBO.getShopId())){
			sql.append(" and r.shop_id = ?");
			params.setString(ordersRefundBO.getShopId());
		}
		if(ordersRefundBO.getStatus() != null){
			sql.append(" and r.status = ?");
			params.setInt(ordersRefundBO.getStatus());
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getNickName())){
			sql.append(" and u.nick_name like ?");
			params.setString("%"+ordersRefundBO.getNickName()+"%");
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getOrderNumber())){
			sql.append(" and o.order_number like ?");
			params.setString("%"+ordersRefundBO.getOrderNumber()+"%");
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getRefundNumber())){
			sql.append(" and r.refund_number like ?");
			params.setString("%"+ordersRefundBO.getRefundNumber()+"%");
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getStartTime())){
			sql.append(" and r.created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersRefundBO.getStartTime())));
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getEndTime())){
			sql.append(" and r.created_time <= ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersRefundBO.getEndTime())));
		}
		sql.append(" order by r.created_time desc");
		
		return super.queryForPagingList(sql.toString(), OrdersRefundBO.class, params,pageNumber,maxPageRow);
	}

	@Override
	public int getTotalCount(OrdersRefundBO ordersRefundBO) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select  count(*) ");
		sql.append(" from orders_refund r");
		sql.append(" inner join orders_goods og on og.id = r.orders_goods_id");
		sql.append(" inner join orders o on o.id = og.order_id");
		sql.append(" inner join goods g on g.id = og.goods_id");
		sql.append(" left join user u on u.id = r.user_id");
		sql.append(" left join shop s on s.id = r.shop_id");
		sql.append(" where r.is_delete = ?");
		params.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(ordersRefundBO.getShopId())){
			sql.append(" and r.shop_id = ?");
			params.setString(ordersRefundBO.getShopId());
		}
		if(ordersRefundBO.getStatus() != null){
			sql.append(" and r.status = ?");
			params.setInt(ordersRefundBO.getStatus());
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getNickName())){
			sql.append(" and u.nick_name like ?");
			params.setString("%"+ordersRefundBO.getNickName()+"%");
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getOrderNumber())){
			sql.append(" and o.order_number like ?");
			params.setString("%"+ordersRefundBO.getOrderNumber()+"%");
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getRefundNumber())){
			sql.append(" and r.refund_number like ?");
			params.setString("%"+ordersRefundBO.getRefundNumber()+"%");
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getStartTime())){
			sql.append(" and r.created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersRefundBO.getStartTime())));
		}
		if(StringUtils.isNotBlank(ordersRefundBO.getEndTime())){
			sql.append(" and r.created_time <= ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersRefundBO.getEndTime())));
		}
		
		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public int getApplyTotal(String shopId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select count(id) from orders_refund where is_delete = ?");
		params.setInt(Is.NO.toIntValue());
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and shop_id = ? and status = ? ");
			params.setString(shopId);
			params.setInt(OrdersRefundStatus.APPLY.toIntStatus());
		}else {
			sql.append(" and status = ?");
			params.setInt(OrdersRefundStatus.SHOP_AGREE.toIntStatus());
		}
		return super.queryForInt(sql.toString(), params);
	}

}
